<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      :error="error"
      error-text="加载失败了，请点击重试"
      @load="onLoad"
    >
      <CommenItem
        v-for="(item, ind) in list"
        :key="ind"
        :comment="item"
        @reply-click="$emit('reply-click', $event)"
      />
      <!-- <van-cell v-for="(item, ind) in list" :key="ind" :title="item.content">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px">{{
              item.pubdate | relativeTime
            }}</span>
            <van-button
              size="mini"
              type="default"
              @click="$emit('reply-click', item)"
              >回复{{ item.reply_count }}</van-button
            >
          </p>
        </div>
        <van-icon
          slot="right-icon"
          :name="item.is_liking ? 'like' : 'like-o'"
          :color="item.is_liking ? '#e5645f' : ''"
          :loading="likeLoading"
          @click="onLikeComment(item)"
        />
      </van-cell> -->
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field clearable placeholder="请输入评论内容">
        <van-button slot="button" size="mini" type="info">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
  </div>
</template>

<script>
import { getComments, addLikeComments, delLikeComments } from '@/api/common';
import CommenItem from './common-item.vue';
export default {
  name: 'ArticleComment',
  components: { CommenItem },
  props: {
    artId: {
      type: [String, Number],
      require: true
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      // list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      limit: 10,
      error: false,
      likeLoading: false
    };
  },
  created () {
    this.onLoad();
  },

  methods: {
    async onLoad () {
      try {
        const { data } = await getComments({
          type: 'a',
          source: this.artId,
          offset: this.offset,
          limit: this.limit
        });
        console.log(data);
        //   1.请求获取数据
        const { results } = data.data;
        this.list.push(...results);
        this.$emit('cmCount', data.data);

        // 2.将数据添加到列表中
        this.loading = false;
        // 3.将loading设置为false
        if (results.length) {
          // 4.判断是否还有数据

          this.offset = data.data.last_id;
          // 有就更新下一页
        } else {
          this.finished = true;
          // 没有就将finished设置为结束
        }
      } catch (error) {
        this.error = true;
        this.loading = false;
      }
    },
    async onLikeComment (obj) {
      this.likeLoading = true;
      try {
        if (obj.is_liking) {
          // 已经点赞了，取消点赞
          await delLikeComments(obj.com_id);
        } else {
          // 没有点赞，那就点赞
          await addLikeComments(obj.com_id);
        }
        obj.is_liking = !obj.is_liking;
      } catch (error) {
        this.$toast('操作失败，请重试');
      }
      this.likeLoading = false;
    }
  }
};
</script>

<style scoped lang="less">
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}

.van-list {
  margin-bottom: 45px;
}
</style>
